package com.Alan.service.user;

import com.Alan.dao.BaseDao;
import com.Alan.dao.user.UserDao;
import com.Alan.dao.user.UserDaoImpl;
import com.Alan.pojo.User;
import com.Alan.util.UtilsAlan;
import org.junit.Test;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateDate 2021/8/9 14:17
 */
public class UserServiceImpl implements UserService{
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode) {
        Connection connection = null;
        User user = null;

        connection = BaseDao.getConnection();
//        通过业务层调用对应的具体数据库
        user = userDao.getLoginUser(connection, userCode);

        BaseDao.closeResource(connection, null, null);
        return user;
    }

    @Override
    public boolean update(int id, String password) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;

        if (userDao.updatePassword(connection, id, password) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);
        return flag;

    }

    @Override
    public int getCount(String userName, int userRole) {
        Connection connection = BaseDao.getConnection();
        int userCount = userDao.getUserCount(connection, userName, userRole);

        BaseDao.closeResource(connection, null, null);
        return userCount;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int startRow, int pageRows) {
        Connection connection = BaseDao.getConnection();
        List<User> userList = userDao.getUserList(connection, userName, userRole, startRow, pageRows);

        BaseDao.closeResource(connection, null, null);
        return userList;
    }

    @Override
    public boolean addUser(User user) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;
        if (userDao.addUser(connection, user) > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public User getUserInformation(int userId) {
        Connection connection = BaseDao.getConnection();
        User user = userDao.getUserInformation(connection, userId);

        BaseDao.closeResource(connection, null, null);
        return user;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;
        if (userDao.updateUser(connection, user) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    @Override
    public boolean delelteUser(int userId) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;
        if (userDao.deleteUser(connection, userId) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
//        方法:getuser()
//        User admin = userService.login("admin");
//        System.out.println(admin.getUserName());
//        System.out.println(admin.getUserPassword());
//        方法:getUserCount
//        int count = userService.getCount("", 2);
//        System.out.println(count);


        /*List<User> userList = userService.getUserList("", 0, 0, 2);
        for (User user : userList) {
            System.out.println(user.toString());
            System.out.println(user.getAge());
        }*/

        /*String strDate = "1999-12-01 00:00:00";
        User user = new User();
        user.setUserCode("test");
        user.setUserName("test");
        user.setUserPassword("test");
        user.setGender("男");
        user.setBirthday(UtilsAlan.strToSqlDate(strDate));
        user.setPhone("12345678911");
        user.setAddress("住在服务器");
        user.setUserRole(3);
        user.setCreatedByUserCode("liming");
        user.setCreatedByUserName("李明");
        user.setCreationDate(UtilsAlan.strToSqlDate("1999-12-1 00:01:01"));
        Boolean flag = userService.addUser(user);
        System.out.println(flag);*/
    }
}
